import random
import re

import argparse


def main():

    """ Главная функция """

    def get_input_parameters():

        """ Получаем входные параметры """

        parser = argparse.ArgumentParser()
        parser.add_argument('-pl', dest='password_length', default=20)
        parser.add_argument('-ec', dest='exclude_characters', default='')
        parser.add_argument('-uln', dest='use_letters_numbers', default='no')

        arguments = parser.parse_args()
        password_length = int(arguments.password_length)
        exclude_characters = arguments.exclude_characters
        use_letters_numbers = arguments.use_letters_numbers

        data = (password_length, exclude_characters, use_letters_numbers)

        return data


    def create_password(password_length, exclude_characters, use_letters_numbers):

        """ Создаем пароль """

        def take_this_symbol(symbol, excluded_characters):

            """ Берем этот символ? """

            if symbol in exclude_characters:

                excluded_characters.append(symbol)

                return 'Не брать'


        def use_only_letters_numbers(symbol, excluded_characters, password):

            """ Используем только цифры и буквы? """

            if re.findall(r'\w', symbol):

                password.append(symbol)
                return 'Подошел'

            else:

                excluded_characters.append(symbol)
                return 'Не подошел'


        # ВЫШЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

        password = []
        count = 0
        excluded_characters = []
        list_symbols = list('1234567890-=!@#$%^&*()_+qwertyuiop[]asdfghjkl;\'zxcvbnm,./QWERTYUIOP[]\ASDFGHJKL;\'ZXCVBNM,./')

        while True:

            symbol = random.choice(list_symbols)

            if take_this_symbol(symbol, excluded_characters) == 'Не брать': continue

            if use_letters_numbers == 'yes':  # Если нужны только буквы и цифры

                well_what = use_only_letters_numbers(symbol, excluded_characters, password)

                if well_what == 'Не подошел': continue

                count += 1
                if count == password_length: break
                continue

            password.append(symbol)
            count += 1
            if count == password_length: break

        excluded_characters = ''.join(excluded_characters)
        password = ''.join(password)

        return (password, excluded_characters)


    # ВЫШЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

    password_length, exclude_characters, use_letters_numbers = get_input_parameters()
    password, excluded_characters = create_password(password_length, exclude_characters, use_letters_numbers)

    print(f'\nНеиспользуемые символы - {excluded_characters}')
    print(f'{len(excluded_characters)}\n')

    print(f'Сгенерированный пароль - {password}')
    print(f'{len(password)}\n')


# ТОЧКА ВХОДА

if __name__ == '__main__':

    main()
